package com.leejw.tradeblogsystem.controller;

import com.leejw.tradeblogsystem.common.Result;
import com.leejw.tradeblogsystem.model.Article;
import com.leejw.tradeblogsystem.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author: jwli3
 * @Desc: 文章管理
 * @create: 2024-08-22
 **/
@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 保存文章
     * @param articleVO
     * @return
     */
    @ResponseBody
    @RequestMapping("/saveArticle")
    public Result saveArticle(@RequestBody Article articleVO) {
        return articleService.saveArticle(articleVO);
    }
    /**
     * 删除文章
     */
    @GetMapping("/deleteArticle")
    public Result deleteArticle(@RequestParam("id") Integer id) {
        return articleService.deleteArticle(id);
    }
    /**
     * 更新文章
     */
    @PostMapping("/updateArticle")
    public Result updateArticle(@Validated @RequestBody Article articleVO) {
        return articleService.updateArticle(articleVO);
    }
    /**
     * 查询所有文章
     */
    @RequestMapping("/findAllArticle")
    public Result findAllArticle() throws Exception {
        Result result = new Result();
        List<Article> list = articleService.findAllArticle();
        result.setData(list);
        return result;
    }
    /**
     * 按月份查询文章用于归档
     */
    @ResponseBody
    @GetMapping("/findArticle")
    public Result findArticle(String createTime) throws Exception {
        Result result = new Result();
        List<Article> list = articleService.findArticle(createTime);
        result.setData(list);
        return result;
    }
    /**
     * 按月份查询文章用于归档
     */
    @ResponseBody
    @GetMapping("/findRecommendArticle")
    public Result findRecommendArticle() throws Exception {
        Result result = new Result();
        List<Article> list = articleService.findRecommendArticle();
        result.setData(list);
        return result;
    }
    /**
     * 按ID查询文章用于展示详情
     */
    @ResponseBody
    @GetMapping("/findArticleById")
    public Result findArticleById(@RequestParam("id") Integer id) throws Exception {
        Result result = new Result();
        Article article = articleService.findArticleById(id);
        result.setData(article);
        return result;
    }
}
